package recyclerview.view;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

import com.lib.recyclyeviewmodule.R;

import recyclerview.HeadFootAdapter;


/**
 * Created by Zmm on 2016/11/17.
 */

public class LoadRecyclerView extends RecyclerView {
    private View empryView;
  //  private int lastPostion;
    private OnScrollListener lScrollListener;
    private OnLoadingMore loadingMore;
    private  int lastLoadPostion=2;//设置滑到最后几个位置刷新数据
    private boolean isCanLoadMore=false;
    LoadMoreFooter footer;
    public LoadRecyclerView(Context context) {
        this(context,null);
    }

    public LoadRecyclerView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LoadRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        super.setOnScrollListener(new MyScrollLisenner());
    }
    public void  setOnScrollListener(OnScrollListener lis){
        this.lScrollListener=lis;
    }

    View footerView;
    @Override
    public void setAdapter(Adapter adapter) {
        super.setAdapter(adapter);
        if(adapter!=null){
            if(adapter instanceof HeadFootAdapter){
                footer=new LoadMoreFooter(getContext());
                footerView= footer.findViewById(R.id.rl_footer_all);
                footerView.setVisibility(View.GONE);
                ((HeadFootAdapter)adapter).addFooterView(footer);
            }
            MyAdapterDateObserver observer =new MyAdapterDateObserver();
            adapter.registerAdapterDataObserver(observer);

        }
    }
    Handler handler =new Handler();
    public void closeLoadMore() {
        footer.setState(LoadMoreFooter.NOMAL);
        footerView.setVisibility(View.GONE);
    }

    public class MyAdapterDateObserver extends AdapterDataObserver {
        @Override
        public void onChanged() {
            if(empryView!=null){
                Adapter adapter = getAdapter();
                HeadFootAdapter headFootAdapter=(HeadFootAdapter)adapter;
                if(headFootAdapter.getItemCount()-headFootAdapter.getFooterCount()-headFootAdapter.getHeaderCount()<=0){
                    empryView.setVisibility(View.VISIBLE);
                }else{
                    empryView.setVisibility(View.GONE);
                }
            }
        }
    }
    public void  setEmpityView(View view){
        if(empryView==null){
            this.empryView=view;
        }
    }
    public class MyScrollLisenner  extends OnScrollListener {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if(lScrollListener!=null){
                lScrollListener.onScrolled(recyclerView,dx,dy);
            }

        }

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if(lScrollListener!=null){
                lScrollListener.onScrollStateChanged(recyclerView,newState);
            }
            if(newState== RecyclerView.SCROLL_STATE_IDLE){
                if(getLayoutManager() instanceof LinearLayoutManager){
                    LinearLayoutManager manager = ((LinearLayoutManager) getLayoutManager());//.findLastVisibleItemPosition();
                    int lastPostion = manager.findLastVisibleItemPosition();
                    if(lastPostion>=manager.getItemCount()-lastLoadPostion&&isCanLoadMore){
                        smoothtoLast();
                    }
                }
            }
        }
    }

    public void smoothtoLast(){
        if(footer.getmState()!= LoadMoreFooter.LOADING){
            setFooterState(LoadMoreFooter.LOADING,null);
            final Adapter adapter = getAdapter();
            handler.post(new Runnable() {
                @Override
                public void run() {
                    scrollToPosition(adapter.getItemCount()-1);
                }
            });
            if(loadingMore!=null){
                loadingMore.onLoadMore();
            }
        }
    }

    /**
     * 加载更多失败
     */
    public  void loadMoreFail(){
      setFooterState(LoadMoreFooter.FAIL, new OnClickListener() {
            @Override
            public void onClick(View v) {
                smoothtoLast();
            }
        });
    }

    /**
     * 加载到最后一页
     */
    public void loadMoreEnd(){
      setFooterState(LoadMoreFooter.END,null);
    }

    public void  setLoadMore(boolean b){
        this.isCanLoadMore=b;
    }
    public  void  setLastLoadPostion(int position){
        this.lastLoadPostion=position;
    }

    public interface OnLoadingMore{
        void onLoadMore();
    }
    public void setOnLoadingMore(OnLoadingMore load){
        this.loadingMore=load;
    }
    public void toNight(){
        footer.toNight();
    }
    public void toDay(){
        footer.toDay();
    }

    public void  setFooterState( int state,OnClickListener lis){
        View footerView = footer.findViewById(R.id.rl_footer_all);
        footerView.setVisibility(View.VISIBLE);
        footer.setState(state);
        footer.setOnClickListener(lis);
        if(state== LoadMoreFooter.END){
            setLoadMore(false);
        }
    }
}
